package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsUser;
import com.yidu.logisticssystem.entity.vo.Treeindex;
import com.yidu.logisticssystem.service.LogisticsUserService;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表(LogisticsUser)表控制层
 *
 * @author makejava
 * @since 2021-03-09 16:21:11
 */
@RestController
@RequestMapping("logisticsUser")
@Api(tags = "物流用户接口")
public class LogisticsUserController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsUserService logisticsUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据ID查询用户类",notes = "返回唯一用户类")
    @PostMapping("selectOne/{id}")
    public BaseMessage selectOne(@PathVariable Integer id) {
        //new 一个BaseMessage
        BaseMessage bm=new BaseMessage();
        //判断id是否为空
        if (id==null){
            bm.setState(0);
            bm.setMessage("ID为空");
        }else {
            //不为空就根据ID查询，返回一个用户对象
            LogisticsUser logisticsUser=logisticsUserService.queryById(id);
            bm.setState(1);
            bm.setMessage("找到啦");
            bm.setData(logisticsUser);
        }
        return bm;
    }

    @ApiOperation(value = "根据sessionID查询用户类",notes = "返回唯一用户类")
    @GetMapping("selectuh")
    public BaseMessage selectuh(HttpSession session) {
        Integer id= (Integer) session.getAttribute("userId");
        LogisticsUser user=logisticsUserService.queryById(7);
        return BaseMessage.succes(user);
    }


    /**
     * 查询所有用户类，然后分类
     * @param logisticsUser
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("查询所有用户类,然后分页,返回一个集合")
    @GetMapping("findAll")
    public Map<String,Object> findAll(LogisticsUser logisticsUser, Integer page, Integer limit){
        //将参数传入到分页助手类中
        PageHelper.startPage(page,limit);
        //new一个map
        Map<String,Object>map=new HashMap<>();
        //查询所有用户类
        List<LogisticsUser> list=logisticsUserService.queryAll(logisticsUser);
        //查询到集合后，将集合数据封装到分页助手的pageinfo中
        PageInfo<LogisticsUser>pageInfo=new PageInfo(list);
        //查询行数
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);
        //传行数到前端页面 pageinfo.getTotal()
        map.put("count",pageInfo.getTotal());
        return map;
    }

    /**
     * 根据ID删除用户类
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除用户类,返回一个唯一数")
    @PostMapping("delete/{id}")
    public BaseMessage delete(@PathVariable Integer id){
        //定义一个布尔值 根据ID删除用户类，返回一个行数
        boolean flag=logisticsUserService.deleteById(id);
        if (flag){
            return BaseMessage.succes();
        }else {
            return BaseMessage.fail();
        }
    }

    /**
     * 根据ID批量删除用户类
     * @param ids  //ids是字符串类型
     * @return
     */
    @ApiOperation("根据ID批量删除用户类,返回一个rows")
    @PostMapping("deleteAll")
    public BaseMessage deleteAll(String ids){
        BaseMessage bm=new BaseMessage();
        int rows=logisticsUserService.deleteAll(ids);
        if (rows>0){
            bm.setState(1);
        }else {
            bm.setState(0);
        }
        return bm;

    }

    /**
     * 查询唯一账号
     * @param logisticsUser
     * @return
     */
    @ApiOperation("查询唯一账号")
    @PostMapping("queryByUserName")
    public BaseMessage queryByUserName(LogisticsUser logisticsUser){
        LogisticsUser logisticsUser1=logisticsUserService.queryByUserName(logisticsUser);
        if (logisticsUser1==null){
            return BaseMessage.succes();
        }else {
            return BaseMessage.fail();
        }
    }

    /**
     * 根据ID判断是增加还是修改方法，返回一个唯一数
     * @param logisticsUser
     * @return
     */
    @ApiOperation("根据ID判断是增加还是修改方法，返回一个唯一数")
    @PostMapping("addOrUpdate")
    public BaseMessage addOrUpdate(LogisticsUser logisticsUser){
        System.out.println(logisticsUser);
        BaseMessage bm=new BaseMessage();
        int rows=0;
        if (logisticsUser.getUserId()==null){
            rows=logisticsUserService.insertSelective(logisticsUser);
        }else {
            rows=logisticsUserService.updateByPrimaryKeySelective(logisticsUser);
        }
        if (rows>0){
            bm.setState(1);
            bm.setMessage("成功!");
        }else {
            bm.setState(0);
            bm.setMessage("失败!");
        }
        return bm;
    }


    /**
     * 登录方法
     * @param logisticsUser
     * @return
     */
    @ApiOperation("登录方法")
    @PostMapping("login")
    //登录的时候必须要传一个request，或者session，用来登录的时候传对象到session中
    public BaseMessage login(LogisticsUser logisticsUser, HttpServletRequest request){
        BaseMessage bm=new BaseMessage();
        System.out.println("=====登录登录方法了");
        LogisticsUser logisticsUser1=logisticsUserService.login(logisticsUser);
        if (logisticsUser1!=null){
            System.out.println("====="+logisticsUser1.getUserCompellation());
            HttpSession session = request.getSession();
            session.setAttribute("user",logisticsUser1);
            bm.setState(1);
        }else {
            bm.setState(0);
        }
        return bm;
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
    }

}
